# 1.导包
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
# 2.准备散点数据集
x,y = make_blobs(
    n_samples=1000, # 默认100
    n_features=2,
    centers=[[-1,-1],[0,0],[1,1],[2,2]],#效果默认是3
    cluster_std=[0.4,0.2,0.2,0.3],# 默认1

)
# print(x)
# print(y)
# 3.展示原始数据的散点图
# x[行索引,列索引] x[:,0] 是第一列 x[:,1] 是第二列
plt.scatter(x[:,0],x[:,1])
plt.show()
#
# TODO 4.Kmeans算法API应用
model = KMeans(n_clusters=10)
y_pre = model.fit_predict(x)
print(y_pre)
# TODO 5. 展示聚类后数据的散点效果
plt.scatter(x[:,0],x[:,1],c=y_pre)
plt.show()